package nancal.mp.mvc.ops

import nancal.mp.db.mongo.JenkinsLanguageEnum
import nancal.mp.db.mongo.extend.getAppDeploySetting
import nancal.mp.db.mongo.mor
import nbcp.base.exception.ParameterInvalidException
import nbcp.base.utils.SpringUtil
import nbcp.http.api.DockerClientApi
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/ops/docker")
class OpsDockerController {
    companion object {
        private val logger = LoggerFactory.getLogger(OpsDockerController::class.java)
    }

    @PostMapping("/restart-container")
    fun restart(targetEnv: String, language: JenkinsLanguageEnum, appName: String) {
        var setting = mor.dev.appDeploySetting.getAppDeploySetting(targetEnv);
        var dockerApiUrl = setting.dockerApiUrl;
        if (dockerApiUrl.isNullOrEmpty()) {
            throw ParameterInvalidException("dockerApiUrl为空！");
        }

        var dockerFeignClient = SpringUtil.getBean<DockerClientApi>()
        dockerFeignClient.killContainer(appName)
        dockerFeignClient.startContainer(appName)
    }
}